package com.travel.common.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.travel.common.exception.WorkCode;
import lombok.Data;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/18 10:49
 * @Desc:
 */
@Data
public class BaseResponse {

    private Integer code;

    private Object data;

    /**
     * 是否需要重新登录
     */
    @JSONField(name = "re_login")
    private Boolean reLogin = false;

    private String message;

    /**
     * 返回成功
     * @param data
     * @return
     */
    public static BaseResponse makeSuccess(Object data){
        return make(WorkCode.SUCCESS.getCode(), WorkCode.SUCCESS.getMessage(), data);
    }

    /**
     * 根据错误码返回错误信息
     * @param workCode
     * @return
     */
    public static BaseResponse makeError(WorkCode workCode){
        return make(workCode.getCode(), workCode.getMessage(), null);
    }

    private static BaseResponse make(Integer code, String message, Object data){
        BaseResponse response = new BaseResponse();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }
}
